﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Housing;

public partial class StreetCenter_AddApply_A : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["id"] != null && Request["applyid"] != null && Session["UserId"] != null)
        {
            if (!IsPostBack)
            {
                BindDate();
                BindDocument();
                PageBind();
            }
            lblnoid.Text = Request.QueryString["id"].ToString();
        }
        else
            WebHelper.AlertAndClose("登陆超时或页面出错，请重新进入");
    }

    protected void PageBind()
    {
        string applyid = Request.QueryString["applyid"].ToString();
        //读取申请表信息
        Apply apply = new Apply(applyid);
        if (apply != null)
        {
            txbaddress.Text = apply.apply_address;
            txbphone.Text = apply.apply_phone;
            txbtotleincome.Value = apply.apply_yearincome.ToString();
            txbpeoplenum.Value = apply.apply_peoplenum.ToString();
            txbavgincome.Value = apply.apply_avgincome.ToString();
            txbavgmonthincome.Value = (apply.apply_avgincome / 12).ToString();
            txbtotleproperty.Value = apply.apply_totlepropertity.ToString();
            txbtotlearea.Value = apply.apply_totlearea.ToString();
            txbavgarea.Value = apply.apply_avgarea.ToString();
            ViewState["longid"] = apply.apply_longid;
            txbapplytime.Text = Convert.ToDateTime(apply.apply_time).ToString("yyyy年MM月dd日");
            if (apply.apply_paybycard != "0")
            {
                chkcard.Checked = true;
                chkmoney.Checked = false;
                txbcardnum.Text = apply.apply_paybycard;
            }
            else
                chkmoney.Checked = true;
            rbldoc.SelectedIndex = rbldoc.Items.IndexOf(rbldoc.Items.FindByText(apply.apply_docname));
            show.InnerHtml = rbldoc.SelectedValue;
            txbproofnum.Text = apply.apply_docnum;
            if (!string.IsNullOrEmpty(apply.apply_docdate))
            {
                ddlyear4.SelectedValue = apply.apply_docdate.Substring(0, 4);
                ddlmonth4.SelectedValue = apply.apply_docdate.Substring(4, 2);
				
                //lblDocDate.Text=apply.apply_docdate;
            }
        }
        //读取申请人家庭情况
        SqlDataReader drApplicant = new Applicant().GetApplicantByApplyid(applyid);
        string html =  application.InnerHtml;
        int count = 0;
        while (drApplicant.Read())
        {
            html += "<table width='100%' border='1' cellspacing='0' style='border-top:none; border-bottom:none; border-left:none; border-right:none'  id='AppNode" + count + "'>";
            html += "<tr>";
            html += "<td width='56px' height='50'><input type='text'  name='txbapplytitle" + count + "' style='width:90%' class='UnderlineButton1' value='" + drApplicant["app_title"].ToString() + "'></td>";
            html += "<td width='58px'><input type='text'  name='txbname" + count + "' style='width:90%' class='UnderlineButton1' value='" + drApplicant["app_name"].ToString() + "'></td>";
            if (drApplicant["app_marryed"].ToString() == "已婚")
                html += "<td width='59px'><select   name='ddlmarry" + count + " '><option>已婚</option><option>未婚</option></select></td>";
            else

                html += "<td width='59px'><select  name='ddlmarry" + count + " '><option>" + drApplicant["app_marryed"].ToString() + "</option><option>已婚</option></select></td>";
            html += "<td width='212px'><input type='text'  name='txbunit" + count + "' style='width:90%' class='UnderlineButton1' value='" + drApplicant["app_workunit"].ToString() + "'></td>";
            html += "<td width='214px'><input type='text'  name='txbcard" + count + "' style='width:90%' class='UnderlineButton1' value='" + drApplicant["app_idcard"].ToString() + "'></td>";
            html += "<td width='168px'><input type='text'  name='txbincome" + count + "' style='width:90%' class='UnderlineButton1' value='" + drApplicant["app_income"].ToString() + "' onblur='Totleincome(\"txbincome" + count + "\",\"text_val" + count + "\");OnlyNumber(\"txbincome" + count + "\",\"收入\")'><input type='hidden' id='text_val" + count + "' value='" + drApplicant["app_income"].ToString() + "' /></td>";
            if (drApplicant["app_ispro"].ToString() == "1")
                html += "<td width='72px'>&nbsp;&nbsp;<input    type='checkbox' checked name='ckbispro" + count + "' onclick='CountIsproNum(\"" + count + "\")'></td>";
            else
                html += "<td width='72px'>&nbsp;&nbsp;<input    type='checkbox' name='ckbispro" + count + "' onclick='CountIsproNum(\"" + count + "\")'></td>";
            if (count != 0)
                html += "<td width='38px'><input type='button'  value='删除' name='del" + count + "'  /></td>";
            else
                html += "<td width='38px'></td>";
            html += "</tr></table>";
            count++;
        }
        appcount.Value = count.ToString();
        drApplicant.Close();
        application.InnerHtml = html;
        //读取财产情况
        Property property = new Property(applyid);
        if (property != null)
        {

            txbsecuritie.Text = money1.Value = Convert.ToString(property.pro_securitie);
            txbestate.Text = money2.Value = Convert.ToString(property.pro_estat);
            txbdevice.Text = money3.Value = Convert.ToString(property.pro_device);
            txbothers.Text = money4.Value = Convert.ToString(property.pro_other);
        }
        //读取保障类型情况
        string noid = Request.QueryString["id"].ToString();
        SubsidyRecord subsidyrecord = new SubsidyRecord();
        subsidyrecord.GetNewestSubsidyRecordByNoid(noid);
        if (subsidyrecord.Adj_id > 0)
        {
            ckbsubsidy.Checked = true;
            ViewState["sub"] = true;
            ViewState["area"] = subsidyrecord.Adj_nowarea;
            ViewState["money"] = subsidyrecord.Adj_nowmoney;
            string subhtml = "";
            subhtml = "补贴建筑面积为:" + subsidyrecord.Adj_nowarea;
            subhtml += "<br/>月补贴金额为:" + subsidyrecord.Adj_nowmoney;
            subsidy.InnerHtml = subhtml;
            if (!string.IsNullOrEmpty(subsidyrecord.Adj_stoptime))
            {
                ddlyear1.SelectedValue = subsidyrecord.Adj_stoptime.Substring(0, 4);
                if (subsidyrecord.Adj_stoptime.Substring(4, 1) == "1")
                    ddlmonth1.SelectedValue = subsidyrecord.Adj_stoptime.Substring(4, 2);
                else
                    ddlmonth1.SelectedValue = subsidyrecord.Adj_stoptime.Substring(5, 1);
            }
        }
        ReduceRecord reducerecord = new ReduceRecord();
        reducerecord.GetNewestReduceRecordBynoid(noid);
        if (reducerecord.Adj_id > 0)
        {
            ckbreduce.Checked = true;
            ViewState["red"] = true;
            //ckbreduce.Enabled = false;
            ddlyear2.Enabled = false;
            ddlmonth2.Enabled = false;
            string redhtml = "";
            redhtml = "承&nbsp;&nbsp;租&nbsp;&nbsp;人&nbsp; ：<input type='text'  name='username' class='UnderlineButton1' ='' value='" + reducerecord.Adj_renter + "'><br/>";
            redhtml += "承 租 地 址 ：<input type='text'  name='address' class='UnderlineButton1' ='' value='" + reducerecord.Adj_rentaddress + "'><br/>";
            redhtml += "住房计租面积：<input type='text'  name='area' class='UnderlineButton1' ='' value='" + reducerecord.Adj_nowarea + "'><br/>";
            redhtml += "核定月租金&nbsp;&nbsp;：<input type='text'  name='checkmoney' ='' class='UnderlineButton1' value='" + reducerecord.Adj_nowmoney + "'><br/>";
            redhtml += "月减免租金额：<input type='text' name='reducemoney'  ='' class='UnderlineButton1' value='" + reducerecord.Adj_nowreduce + "'><br/>";
            if (!string.IsNullOrEmpty(reducerecord.Adj_stoptime))
            {
                ddlyear2.SelectedValue = reducerecord.Adj_stoptime.Substring(0, 4);
                if (reducerecord.Adj_stoptime.Substring(4, 1) == "1")
                    ddlmonth2.SelectedValue = reducerecord.Adj_stoptime.Substring(4, 2);
                else
                    ddlmonth2.SelectedValue = reducerecord.Adj_stoptime.Substring(5, 1);

            }
            reduce.InnerHtml = redhtml;
        }
        else
            ckbreduce.Checked = false;
        KindRecord kindrecord = new KindRecord();
        kindrecord.GetNewestKindRecordBynoid(noid);
        if (kindrecord.Adj_id > 0)
        {
            ckbkind.Checked = true;
            ViewState["kind"] = true;
            //ckbkind.Enabled = false;
            ddlyear3.Enabled = false;
            ddlmonth3.Enabled = false;
            string kindhtml = "";
            kindhtml = "房屋座落：<input type='text'  name='houseaddress' ='' class='UnderlineButton1' value='" + kindrecord.Adj_location + "'><br/>";
            kindhtml += "建筑面积：<input type='text'  name='housearea' ='' class='UnderlineButton1' value='" + kindrecord.Adj_nowarea + "'><br/>";
            kindhtml += "使用面积：<input type='text'  name='usearea' ='' class='UnderlineButton1' value='" + kindrecord.Adj_nowuserarea + "'><br/>";
            if (!string.IsNullOrEmpty(reducerecord.Adj_stoptime))
            {
                ddlyear3.SelectedValue = kindrecord.Adj_stoptime.Substring(0, 4);
                if (kindrecord.Adj_stoptime.Substring(4, 1) == "1")
                    ddlmonth3.SelectedValue = kindrecord.Adj_stoptime.Substring(4, 2);
                else
                    ddlmonth3.SelectedValue = kindrecord.Adj_stoptime.Substring(5, 1);
            }
            kind.InnerHtml = kindhtml;
        }
        else
            ckbkind.Checked = false;
        //读取住房状况
        string houhtml = houses.InnerHtml;
        int houcount = 0;
        int mmm = 1;
        SqlDataReader drhou = new HouseSituation().GetHouseSituationByapplyid(applyid);
        while (drhou.Read())
        {
            houhtml += "<table width='100%' border='1' cellspacing='0'  style='border-top:none; border-bottom:none; border-left:none; border-right:none' id='HousingNode" + houcount + "'>";
            houhtml += "<tr>";
            houhtml += "<td width='25px' style='height: 37px'>" + mmm + "&nbsp;</td>";
            houhtml += "<td style='width:175px;height:37px;'><input type='text'  name='txblocal" + houcount + "' value='" + drhou["hou_address"].ToString() + "' class='UnderlineButton1'  style='width:150px'></td>";
            houhtml += "<td style='height:37px;width:80px;'><input type='text'  name='txbarea" + houcount + "' value='" + drhou["hou_area"].ToString() + "' id='txbarea" + houcount + "' class='UnderlineButton1' style='width:45px' onblur='OnlyNumber(\"txbarea" + houcount + "\",\"建筑面积\")'></td>";
            houhtml += "<td style='height: 37px; width: 87px;'><input type='text'  name='txbowner" + houcount + "' value='" + drhou["hou_owner"].ToString() + "' class='UnderlineButton1' style='width:60px'></td>";
            houhtml += "<td style='width: 129px; height: 37px;'><input type='text'  name='txbownproof" + houcount + "' value='" + drhou["hou_ownproof"].ToString() + "' class='UnderlineButton1' style='width:100px'></td>";
            houhtml += "<td style='width: 149px; height: 37px;'><input type='text'  name='txbnature" + houcount + "' value='" + drhou["hou_nature"].ToString() + "' class='UnderlineButton1' style='width:100px'></td>";
            houhtml += "<td style='width: 161px; height: 37px;'><input type='text'  name='txbremark" + houcount + "' value='" + drhou["hou_remark"].ToString() + "' class='UnderlineButton1' style='width:150px'></td>";
            if (drhou["hou_addcounted"].ToString() == "1")
                houhtml += "<td style='width: 78px; height: 37px;'><input type='checkbox'  checked name='ckbaddcount" + houcount + "' onclick='getTotal()'></td>";
            else
                houhtml += "<td style='width: 78px; height: 37px;'><input type='checkbox'  name='ckbaddcount" + houcount + "' onclick='getTotal()'></td>";
            if (houcount != 0)
                houhtml += "<td style='width: 42px; height: 37px;'><input type='button'  value='删除' name='del" + houcount + "' onclick='DelRowHousing(" + houcount + ");'/></td>";
            else
                houhtml += "<td style='width: 42px; height: 37px;'></td>";
            houhtml += "</tr></table>";
            houcount++;
            mmm++;
        }
        mmmm.Value = mmm.ToString();
        housecount.Value = houcount.ToString();
        drhou.Close();
        houses.InnerHtml = houhtml;
    }
    protected void BindDocument()
    {
        Documents document = new Documents();
        SqlDataReader dr = document.GetAllDocuments();
        int i = 0;
        while (dr.Read())
        {
            rbldoc.Items.Insert(i, new ListItem(dr["doc_name"].ToString(), dr["doc_type"].ToString()));
            i++;
        }
        dr.Close();
    }
    protected void BindDate()
    {
        int year = Convert.ToInt32(DateTime.Now.Year.ToString());
        for (int i = year - 2; i < year + 2; i++)
        {
            ddlyear1.Items.Add(i.ToString());
            ddlyear2.Items.Add(i.ToString());
            ddlyear3.Items.Add(i.ToString());
            ddlyear4.Items.Add(i.ToString());
        }
        for (int i = 1; i < 13; i++)
        {
            ddlmonth1.Items.Add(i.ToString());
            ddlmonth2.Items.Add(i.ToString());
            ddlmonth3.Items.Add(i.ToString());
            ddlmonth4.Items.Add(i.ToString());
        }
        ddlyear1.SelectedValue = DateTime.Now.Year.ToString();
        ddlyear2.SelectedValue = DateTime.Now.Year.ToString();
        ddlyear3.SelectedValue = DateTime.Now.Year.ToString();
        ddlyear4.SelectedValue = DateTime.Now.Year.ToString();

        ddlmonth1.SelectedValue = DateTime.Now.Month.ToString();
        ddlmonth2.SelectedValue = DateTime.Now.Month.ToString();
        ddlmonth3.SelectedValue = DateTime.Now.Month.ToString();
        ddlmonth4.SelectedValue = DateTime.Now.Month.ToString();
    }
 
    protected void btnapply_Click(object sender, EventArgs e)
    {

    }
}
